package com.egg.auth.service.role.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.egg.auth.entity.role.RoleLinkDept;
import com.egg.auth.mapper.role.RoleLinkDeptMapper;
import com.egg.auth.service.role.IRoleLinkDeptService;
import com.egg.common.mybatis.service.impl.BaseServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;


/**
 * @author east
 * @since 2023-07-01
 */
@Slf4j
@Service
public class RoleLinkDeptServiceImpl extends BaseServiceImpl<RoleLinkDeptMapper, RoleLinkDept> implements IRoleLinkDeptService {


    @Override
    public List<RoleLinkDept> findByRoleId(Serializable roleId) {
        if (ObjectUtil.isEmpty(roleId)) {
            return Collections.emptyList();
        }
        return findByRoleId(Arrays.asList(roleId));
    }

    @Override
    public List<RoleLinkDept> findByRoleId(Collection<? extends Serializable> roleId) {
        if (ObjectUtil.isEmpty(roleId)) {
            return Collections.emptyList();
        }
        LambdaQueryWrapper<RoleLinkDept> qw = Wrappers.lambdaQuery();
        qw.in(RoleLinkDept::getRoleId, roleId);
        return list(qw);
    }

    @Override
    public List<RoleLinkDept> findByDeptId(Collection<? extends Serializable> deptId) {
        if (ObjectUtil.isEmpty(deptId)) {
            return Collections.emptyList();
        }
        LambdaQueryWrapper<RoleLinkDept> qw = buildLambdaSortQw();
        qw.in(RoleLinkDept::getDeptId, deptId);
        return list(qw);
    }

    @Override
    public void delByRoleId(Serializable roleId) {
        delByRoleId(Arrays.asList(roleId));
    }

    @Override
    public void delByRoleId(Collection<? extends Serializable> roleId) {
        if (ObjectUtil.isEmpty(roleId)) {
            return;
        }
        LambdaQueryWrapper<RoleLinkDept> qw = buildLambdaQw();
        qw.in(RoleLinkDept::getRoleId, roleId);
        remove(qw);
    }

    @Transactional(rollbackFor = Throwable.class)
    @Override
    public void delByDeptId(Collection<? extends Serializable> deptId) {
        if (ObjectUtil.isEmpty(deptId)) {
            return;
        }
        LambdaQueryWrapper<RoleLinkDept> qw = buildLambdaQw();
        qw.in(RoleLinkDept::getDeptId, deptId);
        remove(qw);
    }

    @Transactional(rollbackFor = Throwable.class)
    @Override
    public void put(Long roleId, List<Long> deptIdList) {
        delByRoleId(roleId);
        if (ObjectUtil.isEmpty(deptIdList)) {
            return;
        }
        List<RoleLinkDept> list = new ArrayList<>();
        for (Long deptId : deptIdList) {
            RoleLinkDept roleLinkDept = new RoleLinkDept();
            roleLinkDept.setRoleId(roleId);
            roleLinkDept.setDeptId(deptId);
            list.add(roleLinkDept);
        }
        baseSave(list);
    }


}
